

export const SET_POSITION = 'setPosition'
export const SET_USERINFO = 'setUserInfo'

export const SET_ORDERS = 'setOrders'
export const ADD_CART = 'addCart'
export const REDUCE_CART = 'reduceCart'

export default {

    [SET_POSITION](state, position) {
        state.position = position
    },
    [SET_USERINFO](state, userInfo) {
        state.userInfo = userInfo
    },

    [ADD_CART](state, { shopId, foodId, skuId }) {
        let shopOrders = state.orders[shopId]
        if (!shopOrders) {
            shopOrders = state.orders[shopId] = {}
        }

        let foodOrders = shopOrders[foodId]
        if (foodOrders) {
            shopOrders[foodId] = { ...foodOrders, [skuId]: (foodOrders[skuId] || 0) + 1 }
        } else {
            shopOrders[foodId] = { [skuId]: 1 }
        }

        state.orders = { ...state.orders }
    },
    [REDUCE_CART](state, { shopId, foodId, skuId }) {
        let shopOrders = state.orders[shopId]
        if (!shopOrders) {
            return
        }

        let foodOrders = shopOrders[foodId]
        if (!foodOrders) {
            return
        }

        if (!foodOrders[skuId]) {
            return
        }

        shopOrders[foodId] = { ...foodOrders, [skuId]: foodOrders[skuId] - 1 }
        if (foodOrders[skuId] === 0) {
            delete foodOrders[skuId]
        }

        state.orders = { ...state.orders }
    },

    [SET_ORDERS](state, data) {
        state.orders = data
    }
}

